package com.netty.demo;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 * @Auther: Admin
 * @Date: 2021/4/14 11:21
 * @Description:
 */
public class TestByteBufferTransfer {

    public static void main(String[] args) {

        try (FileChannel inputChannel = new FileInputStream("from.txt").getChannel();
             FileChannel outputChannel = new FileOutputStream("to.txt").getChannel();
        ) {

//            inputChannel.transferTo(0, inputChannel.size(), outputChannel);// 底层使用的是OS的零拷贝

            long size = inputChannel.size();
            for (long left = size; left > 0;){// 解决2g文件大小的限制

                left -=inputChannel.transferTo(size - left, left, outputChannel);

            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
